1
00:00:00,090 --> 00:00:00,620
All right.

2
00:00:00,630 --> 00:00:09,140
Let's talk about functions now functions are what I like to think of as many programs.

3
00:00:09,150 --> 00:00:14,970
It's like this organized block of code that you you define and then you can call it later and makes

4
00:00:14,990 --> 00:00:17,460
a lot simpler than typing everything out.

5
00:00:17,910 --> 00:00:21,330
So it's a lot easier to visualize this than me to just talk.

6
00:00:21,330 --> 00:00:22,360
The theory of it.

7
00:00:22,440 --> 00:00:28,830
So let's go ahead and let's g r our script again our script up high and then we're going to Ampersand

8
00:00:28,830 --> 00:00:37,410
this one this time and we should have our python script from before I'm Mrs. scroll down to the bottom

9
00:00:38,100 --> 00:00:43,790
and I'm going to add in a new section here in that section and just going to be called functions.

10
00:00:43,920 --> 00:00:51,630
So if we want to print out we can print out a new line to kind of put this together and then maybe when

11
00:00:51,630 --> 00:00:58,530
it's all said and done we can print a function or make a function that just prints and make it a little

12
00:00:58,530 --> 00:00:59,490
bit easier for us.

13
00:00:59,520 --> 00:01:04,410
So we're going to print a line here defined functions and maybe we'll just print out something that

14
00:01:04,410 --> 00:01:09,460
says Here is an example.

15
00:01:09,480 --> 00:01:13,690
Function so this is really just going to define the area.

16
00:01:13,720 --> 00:01:21,400
If we save it while you're typing years I'm just going to print mine so well Python 3 script that pi

17
00:01:22,270 --> 00:01:26,770
and you see we've got the new line here and we see here is an example function and we just start from

18
00:01:26,770 --> 00:01:27,840
here.

19
00:01:27,940 --> 00:01:33,230
So let's take a look at a function using something that we've already done before.

20
00:01:33,710 --> 00:01:41,200
So we've got this section here and we've got name age GPA we're going to kind of reuse this and this

21
00:01:41,210 --> 00:01:42,640
print my name is.

22
00:01:42,980 --> 00:01:45,670
So let's go ahead and reuse this.

23
00:01:45,740 --> 00:01:48,780
We're gonna say function here.

24
00:01:48,780 --> 00:01:49,000
Right.

25
00:01:49,030 --> 00:01:50,830
So we're going to define kind of.

26
00:01:50,860 --> 00:01:53,150
Who am I.

27
00:01:53,200 --> 00:02:00,250
So we're going to define this and then we're going to do two parentheses and then a colon here.

28
00:02:00,250 --> 00:02:02,470
So it's going to look like this.

29
00:02:02,470 --> 00:02:06,760
And what's important now is that we use indentation.

30
00:02:06,760 --> 00:02:14,230
Now we haven't had to talk about indentation to this point but python is very very critical on utilizing

31
00:02:14,230 --> 00:02:14,980
indentation.

32
00:02:14,980 --> 00:02:19,030
If you don't use it in the right places then your program will not function.

33
00:02:19,060 --> 00:02:24,780
So it's very important to know when to use indentation into indent properly so to indent.

34
00:02:24,790 --> 00:02:32,560
I'm just going to tab here and I'm going to redefine everything again so we can just say name equals

35
00:02:32,620 --> 00:02:44,770
Pete and we can just say age equals 30 type that wrong there and then we can just copy this print right

36
00:02:44,770 --> 00:02:47,630
here so we don't have to type it all out again.

37
00:02:47,650 --> 00:02:52,160
Copy and paste it and you can see define who am I.

38
00:02:52,710 --> 00:02:58,530
And we're going to define in this function a name variable and age variable and then we're gonna print

39
00:02:58,530 --> 00:02:59,190
this out.

40
00:02:59,820 --> 00:03:08,370
So if we were to just go ahead and save this and print this Nothing nothing's here.

41
00:03:08,600 --> 00:03:11,960
All we've done so far is just define this function.

42
00:03:11,960 --> 00:03:19,460
We have done nothing to actually call it or or do anything and let's make a note here that this is a

43
00:03:19,460 --> 00:03:20,030
function.

44
00:03:20,050 --> 00:03:20,390
Right.

45
00:03:21,320 --> 00:03:26,290
So when we get down towards the bottom let's go ahead and just try to call this function.

46
00:03:26,330 --> 00:03:33,560
So to call a function all we got to say is Who am I and let's go ahead and save this.

47
00:03:33,560 --> 00:03:36,980
Run it again and look.

48
00:03:36,980 --> 00:03:39,230
It says My name is Heath and I am 30 years old.

49
00:03:39,230 --> 00:03:41,120
Well what's happening here.

50
00:03:41,150 --> 00:03:41,410
OK.

51
00:03:41,450 --> 00:03:45,110
So we're defining a set of things to do.

52
00:03:45,110 --> 00:03:45,730
Right.

53
00:03:45,740 --> 00:03:49,110
This is a mini program inside this mini program.

54
00:03:49,160 --> 00:03:55,670
We've defined our variables and we define an action here to take which is printing out this string.

55
00:03:56,180 --> 00:04:02,120
So what it's doing is when we call this it's saying Hey I'm gonna go ahead and take all this and I'm

56
00:04:02,120 --> 00:04:05,620
going to run this program and give you the result.

57
00:04:05,660 --> 00:04:12,290
Now what's important to know is whatever is stored in these variables here is only stored there inside

58
00:04:12,350 --> 00:04:13,820
the function.

59
00:04:13,820 --> 00:04:19,840
If we were to print age from the last video it should still be thirty two I do believe.

60
00:04:20,030 --> 00:04:27,200
So it's print and you'll see it's 32 even though we declared age up here as 30 that only lives inside

61
00:04:27,200 --> 00:04:28,340
this function.

62
00:04:28,340 --> 00:04:30,420
So I'm going to go ahead and delete this.

63
00:04:30,560 --> 00:04:35,630
So again everything is living inside this function and it's a mini little program that we have here

64
00:04:35,630 --> 00:04:37,990
and we just call it at a later time.

65
00:04:38,060 --> 00:04:43,960
So let's go ahead and build out another function and we can kind of start to make sense of this.

66
00:04:44,030 --> 00:04:47,870
So let's start with adding what are called parameters.

67
00:04:47,870 --> 00:04:57,110
And will this do adding parameters here so let's make a function and this function is called add one

68
00:04:57,110 --> 00:05:05,200
hundred like this OK and then the parameter I'm going to actually add inside instead of giving empty

69
00:05:05,230 --> 00:05:08,780
parentheses I'm going to add in something called num.

70
00:05:08,790 --> 00:05:17,330
It's just going to be for a number and then we're gonna go ahead and use our colon it enter you a tab.

71
00:05:17,520 --> 00:05:23,760
And then what I want to do is I want to print no floss one hundred.

72
00:05:24,430 --> 00:05:25,840
So what am I doing here.

73
00:05:25,840 --> 00:05:28,030
I'm taking a number and I'm adding 100 to it.

74
00:05:28,060 --> 00:05:32,950
So my function of adding one hundred makes logical sense.

75
00:05:32,950 --> 00:05:33,330
OK.

76
00:05:33,340 --> 00:05:43,120
So what's going to happen then if I say ad one hundred and then I specify a number in the parameter.

77
00:05:43,120 --> 00:05:46,130
Well I'm going to say what if I want to add one hundred to one hundred.

78
00:05:46,900 --> 00:05:52,080
Well then I should in theory print out two hundred right.

79
00:05:52,250 --> 00:05:53,670
And there you go.

80
00:05:53,710 --> 00:06:01,500
We have successfully printed out two hundred so we can build upon this even more so what if we want

81
00:06:01,500 --> 00:06:04,230
to use multiple parameters.

82
00:06:04,230 --> 00:06:13,910
OK let's say we want to do multiple parameters and we want to define an add function.

83
00:06:13,920 --> 00:06:17,720
We're just gonna make an ad even that's built into Python we're gonna make our own.

84
00:06:18,180 --> 00:06:21,360
So we're just going to say add x and y.

85
00:06:21,360 --> 00:06:26,040
So now we have two parameters here going add our colon.

86
00:06:26,190 --> 00:06:31,770
Make sure we indent and then we're just gonna say print x plus y.

87
00:06:32,970 --> 00:06:44,080
Now if we go in and we add seven and seven we should get 14 and there's 14.

88
00:06:44,410 --> 00:06:46,450
So hopefully this is starting to make sense.

89
00:06:46,480 --> 00:06:52,340
All we're doing is building these mini programs and we can have no parameters as we saw here.

90
00:06:52,390 --> 00:06:57,560
We can have single parameters or multiple parameters depending on what we need.

91
00:06:57,580 --> 00:07:00,640
So let's go ahead and build a couple more out.

92
00:07:00,640 --> 00:07:02,360
Just to make sense of it.

93
00:07:02,410 --> 00:07:12,160
So what if we wanted to define multiply and we'll just say x y again.

94
00:07:12,170 --> 00:07:20,650
What do you think that's going to look like well instead of saying print what if we say return and throw

95
00:07:20,650 --> 00:07:25,720
something new at you going to say return x and y OK.

96
00:07:25,780 --> 00:07:36,140
Now what happens if I call multiply and I say 7 7 again what happens here.

97
00:07:36,250 --> 00:07:37,430
Go ahead and save it.

98
00:07:39,560 --> 00:07:41,180
Well nothing happened.

99
00:07:41,210 --> 00:07:42,830
We're not printing out to the screen.

100
00:07:42,830 --> 00:07:44,770
All we're doing is returning.

101
00:07:44,810 --> 00:07:48,950
So when I'm pulling this multiply in it's returning a certain number.

102
00:07:49,250 --> 00:07:52,850
So I'm calling it here and it's saying hey seven times seven.

103
00:07:52,850 --> 00:07:54,360
That's forty nine.

104
00:07:54,530 --> 00:07:57,430
But I have no idea what you want me to do with it.

105
00:07:57,620 --> 00:08:09,020
Now I could say branch multiply here and then we can see what happens.

106
00:08:09,530 --> 00:08:11,480
And now you see it's forty nine.

107
00:08:11,640 --> 00:08:17,490
So the return feature just allows us to return the number that is back to us.

108
00:08:17,490 --> 00:08:17,730
Right.

109
00:08:17,730 --> 00:08:20,400
We don't have to always print it out if we don't want to.

110
00:08:20,400 --> 00:08:22,110
We can store it for later.

111
00:08:22,110 --> 00:08:23,700
So that's what we can do here.

112
00:08:23,730 --> 00:08:24,900
So we don't always have to print it.

113
00:08:24,900 --> 00:08:25,560
We can.

114
00:08:25,560 --> 00:08:28,310
We can return the number and then call it later.

115
00:08:28,320 --> 00:08:30,630
So that's one way of looking at it.

116
00:08:30,660 --> 00:08:31,860
Let's do one more.

117
00:08:31,860 --> 00:08:36,820
How about we define square it

118
00:08:39,640 --> 00:08:43,660
and we only take one number here.

119
00:08:44,180 --> 00:08:48,050
So how would we do the square root of something.

120
00:08:48,050 --> 00:08:57,950
Well let's go ahead and just print out x and then we're going to do by point five.

121
00:08:57,950 --> 00:09:03,080
Remember the exponent with two multiplications here and then we're doing the square root.

122
00:09:03,110 --> 00:09:07,190
So we're going to take point five instead of squared which would be two.

123
00:09:07,220 --> 00:09:07,550
Right.

124
00:09:07,550 --> 00:09:10,210
So we're taking the square root do point five.

125
00:09:10,220 --> 00:09:15,200
And let's take an easy one Let's return the square root of sixty four.

126
00:09:15,320 --> 00:09:24,060
So let's say square root of sixty four and see what happens.

127
00:09:24,970 --> 00:09:27,160
And it returns eight for us.

128
00:09:27,170 --> 00:09:31,510
Now we can make that integer if we want to and go from there.

129
00:09:31,550 --> 00:09:41,640
But that's just a nice base example so lastly I mentioned that we can make a function for a new line

130
00:09:41,670 --> 00:09:46,680
because we've been sitting in here and we've been just typing print and I am slow at it.

131
00:09:46,680 --> 00:09:51,720
I have to sit here and think about it and then I find the correct letters and this is how you print

132
00:09:51,720 --> 00:09:52,940
new line.

133
00:09:52,980 --> 00:09:53,360
OK.

134
00:09:53,370 --> 00:09:54,330
I'm not the best at it.

135
00:09:54,360 --> 00:10:05,540
But what if instead we just did something like define new line and then we just put this here.

136
00:10:06,690 --> 00:10:10,440
So then we can just call a new line from here on out.

137
00:10:10,770 --> 00:10:12,210
We could say a new line.

138
00:10:12,300 --> 00:10:20,040
And then when this prince will print out a new line and while it's the same kind of length ish of this

139
00:10:20,490 --> 00:10:25,530
we don't have to type in these special characters look for them we could even shorten this to like an

140
00:10:25,530 --> 00:10:32,490
L something like an L and then make it really simple and then we save time.

141
00:10:32,650 --> 00:10:35,790
And this is where these programs come in and they save time.

142
00:10:35,830 --> 00:10:38,520
So good example of a use of a function.

143
00:10:38,530 --> 00:10:43,690
So I'm going to save this if you want to use this for moving forward in going on in the next videos.

144
00:10:43,870 --> 00:10:45,550
Absolutely welcome to.

145
00:10:45,550 --> 00:10:51,070
So if you're following along go ahead and just leave your text editor open and I will catch you over

146
00:10:51,070 --> 00:10:54,430
in the next video as we start to talk about boolean expressions.
